package easytalk.dialogs;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import common.Discussion;
import common.DiscussionState;
import common.EasyChatEnvironment;

public class ChatDialogManager extends Thread {

	/**
	 * Browser for displaying the wall
	 */
	private Browser wallBrowser=null;
	
	private boolean wallMode = true;
	
	public static ChatDialogManager getInstance() {
		return instance;
	}
	public static void setInstance(ChatDialogManager instance) {
		ChatDialogManager.instance = instance;
	}
	private Map<Discussion, ChatDialog> discussions = new HashMap<Discussion, ChatDialog>();

	private final Object lock = new Object();

	private Display display;

	private static ChatDialogManager instance = new ChatDialogManager();

	public Display getDisplay() {
		return display;
	}

	public void removeDiscussion(Discussion discussion) {
		discussions.remove(discussion);
	}

	@Override
	public void run() {

		while (true) {
			try {
				synchronized (lock) {
					lock.wait();
				}

			} catch (InterruptedException e) {
				e.printStackTrace();
				return;
			}
			display.asyncExec(new Runnable() {

				@Override
				public void run() {
					for (Discussion discussion : EasyChatEnvironment
							.getInstance().getDiscussions()) {
						if  ( ( (discussion.getState() == DiscussionState.OPEN) ||
								(discussion.getState() == DiscussionState.OPEN_WAITING)) && 
								(!wallMode)	) {
							ChatDialog fDialog = discussions.get(discussion);
							if ((fDialog == null)) {
								/*
								 * Display display = PlatformUI.getWorkbench()
								 * .getDisplay();
								 */
								Shell shell = new Shell(display, SWT.MODELESS);
								ChatDialog dialog = new ChatDialog(shell,
										discussion);
								dialog.openInANewthread();
								discussions.put(discussion, dialog);
								System.out.println(Messages.getString("ChatDialogManager.1")); //$NON-NLS-1$
							}
						}
					}
				}

			});
		}

	}

	public void setDisplay(Display display) {
		this.display = display;
	}

	public void updateDialogs() {
		synchronized (lock) {
			lock.notify();
		}

	}

	public void updateDiscussions(Discussion discussion) {
		switch (discussion.getState()) {
		case OPEN:
			if (wallMode) {
				wallBrowser.setText(discussion.getSentences().get(0).getContent());
			}
			else 
			{
			ChatDialog dialog = discussions.get(discussion);
			if (dialog != null) {
				dialog.updateDiscussion(discussion);
			}
			}
			break;
		case OPEN_DESCRETE:
			//wallBrowser.setText(discussion.getSentences().get(0).getContent());
			break;
		}
	}
	
	
	public void setWallBrowser(Browser wallBrowser) {
		this.wallBrowser = wallBrowser;
	}
	
	public Browser getWallBrowser() {
		return wallBrowser;
	}
	public void setWallMode(boolean wallMode) {
		this.wallMode = wallMode;
	}
	public boolean isWallMode() {
		return wallMode;
	}

}
